//
//  BankCarViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "BankCarViewController.h"
#import "AddCarViewController.h"
#import "BankModel.h"

static NSString *cellPath = @"BankCarCell";
@interface BankCarViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSDictionary *bankDic;
@property (nonatomic, strong)NSArray *carAry;

@end

@implementation BankCarViewController

- (void)viewWillAppear:(BOOL)animated
{
//    [super viewWillDisappear:animated];
    [self requestBankCarList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _bankDic = @{@"1002":@"gongshang",@"1005":@"nonghang",@"1026":@"zhongguoyinh",@"1003":@"jianhang",@"1001":@"zhaoshang",@"1066":@"youzhengyinh-",@"1020":@"jiaotong",@"1004":@"pufay",@"1006":@"minsheng",@"1009":@"xingye",@"1010":@"pingan",@"1021":@"zhongxin",@"1025":@"huaxia",@"1027":@"guangfa",@"1022":@"guangda",@"1032":@"beijing",@"1056":@"ningbo"};
    [self creatUI];
}

- (void)creatUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"银行卡";
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-80*displaywidth-SCREER_HeiX-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = BacksColor;
    [self.view addSubview:_tableview];
    
    UIButton *addCarBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , SCREEN_HEIGHT-60*displaywidth-SCREER_HeiX-STA_NAV_HEIGHT, SCREEN_WIDTH-120*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(addCarBtnAction) Title:@"添加银行卡" Font:15.0];
    [addCarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addCarBtn.backgroundColor = ThemeColor;
    [addCarBtn.layer setMasksToBounds:YES];
    [addCarBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:addCarBtn];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return _carAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_carAry.count > 0)
    {
        BankModel *model = _carAry[indexPath.row];
        NSString *backImg = @"zhongguoyinh";
        if ([_bankDic.allKeys containsObject:model.bankId])
        {
            backImg = _bankDic[model.bankId];
        }
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(7.5*displaywidth, 10*displaywidth, SCREEN_WIDTH-15*displaywidth, 110*displaywidth) ImageName:backImg];
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.cornerRadius = 5;
        imgView.layer.masksToBounds = YES;
        [cell.contentView addSubview:imgView];
        UILabel *namelab = [ZCControl createLabelWithFrame:CGRectMake(60*displaywidth, 10*displaywidth, SCREEN_WIDTH-75*displaywidth, 25*displaywidth) Font:15.0 Text:model.bank Color:[UIColor whiteColor]];
        [imgView addSubview:namelab];
        UILabel *carTypelab = [ZCControl createLabelWithFrame:CGRectMake(60*displaywidth, 35*displaywidth, SCREEN_WIDTH-75*displaywidth, 15*displaywidth) Font:12.0 Text:@"" Color:RGB(242, 242, 242)];
        [imgView addSubview:carTypelab];
        NSInteger lenth = model.cardNum.length;
        NSString *carNum = @"0000";
        if (lenth > 4)
        {
            carNum = [model.cardNum substringFromIndex:lenth-3];
            carNum = [NSString stringWithFormat:@"****    ****    ****    %@",carNum];
        }
        UILabel *carNumlab = [ZCControl createLabelWithFrame:CGRectMake(0, 60*displaywidth, SCREEN_WIDTH-15*displaywidth, 30*displaywidth) Font:22.0 Text:carNum Color:[UIColor whiteColor]];
        carNumlab.textAlignment = NSTextAlignmentCenter;
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:carNum];
        [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(carNum.length-3, 3)];
        carNumlab.attributedText= aString;
        [imgView addSubview:carNumlab];
    }
    

    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BankModel *model = _carAry[indexPath.row];
    WEAKSELF;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"解除绑定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSDictionary *dic = @{@"id":[NSString stringWithFormat:@"%@",model.ID]};
        [HYBaseHandler requestBankCarddeleteParmers:dic Success:^(id obj) {
            if ([obj server_Getcodecheck])
            {
                [LTools showMBProgressWithText:@"解绑成功" addToView:weakSelf.view];
                [weakSelf requestBankCarList];
            }
            else
            {
                [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
            }

        } failed:^(NSError *error) {

        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:okAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120*displaywidth;
}

- (void)addCarBtnAction
{
    AddCarViewController *vc = [[AddCarViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestBankCarList
{
    WEAKSELF;
    [HYBaseHandler requestBankCarListParmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             weakSelf.carAry = [BankModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf.tableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
         
     } failed:^(NSError *error) {
         
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
